﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FineUICore;
using Microsoft.AspNetCore.Mvc;
using MyAppBox.Controllers;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;

namespace MyAppBox.Areas.system.Controllers
{
    [Area("system")]
    public class system7Controller : SysPageController
    {
        // GET: system/system7
        public ActionResult Index()
        {
            InitData();
            return View();
        }
        #region List代码

        /// <summary>
        /// 
        /// </summary>
        private void InitData()
        {
            DataTable dt = new DataTable();
            dt = GetTreeData(null);
            IList<TreeNode> nodes = new List<TreeNode>();
            nodes = FineUIEx.OnInitTreeDataBind(dt, "SYSTEM_7_20", "SYSTEM_7_10", "SYSTEM_7_60");
            ViewBag.ddlsys760DataSource = FineUIEx.GetNewTreeDt(dt, "SYSTEM_7_10", "SYSTEM_7_60");
            ViewBag.TreeMenuNodes = nodes.ToArray();
            ViewBag.ddlsys730DataSource = FineUIEx.GetNewTreeDt(GetSys6TreeData(null), "SYSTEM_6_10", "SYSTEM_6_70");

        }

        /// <summary>   
        /// 得到菜单树信息
        /// </summary>
        /// <param name="msq"></param>
        /// <returns></returns>
        private DataTable GetTreeData(MsQuery msq)
        {
            if (msq == null) { msq = new MsQuery(); }
            //条件
            if (true)
            {
                msq.strquery += " and 1=1 and SYSTEM_7_40='1' ";
                msq.strorder = " SYSTEM_7_100 ";
            }
            //ms.strorder = "  ";//排序语句
            DataTable dt = ((DataSet)new BLLsystem_7().Getsystem_7ByQuery(msq).data).Tables[0];
            return dt;
        }

        private DataTable GetSys6TreeData(MsQuery msq)
        {
            MsQuery ms = new MsQuery();
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 and SYSTEM_6_30='1' ";
            }
            //ms.strorder = "  ";//排序语句
            DataTable dt = ((DataSet)new BLLsystem_6().Getsystem_6ByQuery(ms).data).Tables[0];
            return dt;
        }

        /// <summary>
        /// 节点选择
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult treeMenu_NodeClick(string nodeId)
        {
            Mssystem_7 ms = new BLLsystem_7().Getsystem_7MsByKey(nodeId);
            StringBuilder sb = new StringBuilder();
            sb.Append("F.ui.EditForm.setdata(" + JObject.FromObject(ms).ToString() + ");");
            PageContext.RegisterStartupScript(sb.ToString());

            //系统标识的菜单不能修改， 删除
            Mssystem_1 mssys1 = GetSession<Mssystem_1>("LoginUser");
            if (mssys1 != null && !mssys1.isadmin && ms.System_7_120 == "0")
            {


                UIHelper.Button("btnedit").Enabled(false);
                UIHelper.Button("btndelete").Enabled(false);
            }
            else
            {
                UIHelper.Button("btnedit").Enabled(true);
                UIHelper.Button("btndelete").Enabled(true);
            }

            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult btnsave_Click(JObject ms)
        {
            Mssystem_7 ms7 = (Mssystem_7)ms.ToObject(typeof(Mssystem_7));
            ms7.System_7_40 = "1";
            MsReturned msr = new BLLsystem_7().Savesystem_7(ms7);
            if (Convert.ToInt32(msr.strMsId) >= 0)
            {
                var treeMenu = UIHelper.Tree("treeMenu");
                DataTable dt = GetTreeData(null);
                //绑定树
                IList<TreeNode> nodes = new List<TreeNode>();
                nodes = FineUIEx.OnInitTreeDataBind(dt, "SYSTEM_7_20", "SYSTEM_7_10", "SYSTEM_7_60");
                treeMenu.LoadData(nodes);
                //重置选择
                string strscript = " F.ui.EditForm.DataID='" + msr.strMsId + "';";
                treeMenu.SelectedNodeID(msr.strMsId);
                PageContext.RegisterStartupScript(strscript);
                ShowNotify("保存成功");
            }
            return FineUICore.UIHelper.Result();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult btndelete_Click(string id)
        {
            new BLLsystem_7().Deletesystem_7ByKey(id);
            ShowNotify("删除成功");
            var treeMenu = UIHelper.Tree("treeMenu");
            DataTable dt = GetTreeData(null);
            //绑定树
            IList<TreeNode> nodes = new List<TreeNode>();
            nodes = FineUIEx.OnInitTreeDataBind(dt, "SYSTEM_7_20", "SYSTEM_7_10", "SYSTEM_7_60");
            treeMenu.LoadData(nodes);
            string strscript = "  $(F.ui.treeMenu.el).find('.f-tree-node').eq(1).click();";
            PageContext.RegisterStartupScript(strscript);
            return FineUICore.UIHelper.Result();
        }

        #endregion

    }
}